import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import '/router/router.dart';

class LaunchPage extends StatefulWidget {
  LaunchPage({Key? key}) : super(key: key);

  @override
  _LaunchPageState createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
  int _currentIndex = 0;
  bool _jumpFlag = false;
  late SwiperController _controller;
  List<Map> imgList = [
    {
      "url": "images/bg2.jpg",
    },
    {
      "url": "images/bg1.jpg",
    },
    {
      "url": "images/bg.png",
    },
  ];
  @override
  void initState() {
    super.initState();
    _controller = new SwiperController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.miniEndTop,
      floatingActionButton: _jumpFlag ? FloatButton() : null,
      body: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return new Image.asset(
            imgList[index]["url"],
            fit: BoxFit.fill,
          );
        },
        itemCount: imgList.length,
        pagination: new SwiperPagination(),
        autoplay: !_jumpFlag,
        controller: _controller,
        index: _currentIndex,
        onIndexChanged: (int index) {
          setState(() {
            _currentIndex = index;
          });
          if (index == 2) {
            setState(() {
              _jumpFlag = true;
            });
          }
        },
        // control: new SwiperControl(),
      ),
    );
  }
}

class FloatButton extends StatelessWidget {
  const FloatButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      width: 40,
      padding: EdgeInsets.all(4),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        color: Colors.white,
      ),
      child: FloatingActionButton(
        child: Icon(Icons.arrow_forward),
        onPressed: () async {
          Get.offAllNamed(RouteConfig.main);
        },
      ),
    );
  }
}
